﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace pgen
{
    public class SerialCodeGenerator: IEnumerable<int>
    {
        public int SerialLength { get; set; }
        
        public int SerialStart { get; set; }

        public int Count { get; set; }

        public SerialCodeGenerator()
        {
            SerialLength = 4;
            SerialStart = 1000;
            Count = 10;
        }

        public IEnumerator<int> GetEnumerator()
        {
            return new SerialCodeEnumerator(this);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public class SerialCodeEnumerator: IEnumerator<int>
    {
        private readonly int _length, _start, _maxVal;
        private int _currVal, _count;

        public SerialCodeEnumerator(SerialCodeGenerator generator)
        {
            _length = generator.SerialLength;
            _start = generator.SerialStart;
            _count = generator.Count;
            
            var s = "";
            for (var i = 0; i < _length; i++)
            {
                s += "9";
            }
            _maxVal = int.Parse(s);
            Reset();
        }

        public void Dispose()
        {
            // do nothing
        }

        public bool MoveNext()
        {
            return (--_count >= 0);
        }

        public void Reset()
        {
            _currVal = _start;
        }

        public int Current
        {
            get 
            { 
                _currVal++;
                if (_currVal > _maxVal) Reset();
                return _currVal;
            }
        }

        object IEnumerator.Current
        {
            get { return Current; }
        }
    }
}
